<script setup lang="ts">
import { computed } from 'vue';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { enableStatusOptions } from '@/constants/business';
import { translateOptions } from '@/utils/common';

defineOptions({
  name: 'TenantSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<ManageApi.TenantManage.TenantSearchParams>('model', { required: true });

type RuleKey = Extract<keyof ManageApi.TenantManage.TenantSearchParams, 'tenantEmail' | 'tenantPhone'>;

const rules = computed<Record<RuleKey, CommonType.WithArray<App.Global.FormRule>>>(() => {
  const { patternRules } = useFormRules();

  return {
    tenantEmail: patternRules.email,
    tenantPhone: patternRules.phone
  };
});

async function reset() {
  await restoreValidation();
  emit('reset');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem title="搜索" name="user-search">
        <NForm ref="formRef" :model="model" :rules="rules" label-placement="left" :label-width="80">
          <NGrid responsive="screen" item-responsive>
            <NFormItemGi span="24 s:12 m:6" label="用户名" path="tenantName" class="pr-24px">
              <NInput v-model:value="model.tenantName" placeholder="请输入租户名" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="手机号" path="tenantPhone" class="pr-24px">
              <NInput v-model:value="model.tenantPhone" placeholder="请输入手机号" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="邮箱" path="tenantEmail" class="pr-24px">
              <NInput v-model:value="model.tenantEmail" placeholder="请输入邮箱" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="租户状态" path="userStatus" class="pr-24px">
              <BooleanSelect
                v-model:value="model.status"
                placeholder="请选择租户状态"
                :options="translateOptions(enableStatusOptions)"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi span="24 m:12" class="pr-24px">
              <NSpace class="w-full" justify="end">
                <NButton @click="reset">
                  <template #icon>
                    <icon-local-ic-round-refresh class="text-icon" />
                  </template>
                  重置
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <icon-local-ic-round-search class="text-icon" />
                  </template>
                  搜索
                </NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
